﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace app2
{
    public partial class Form1 : Form
    {
        MyBoll myboll;
        List<Boll> bolls = new List<Boll>();
        SolidBrush sb1 = new SolidBrush(Color.Red);
        SolidBrush sb2 = new SolidBrush(Color.Blue);
        Image image4 = Image.FromFile(@"1.png");
        Image image5 = Image.FromFile(@"xmq.png");
        Graphics g = null;
        int bollnum = 10;
        public Random rand;
        public int timer = 0;
        public Form1()
        {
            InitializeComponent();
            this.DoubleBuffered = true;  //避免闪烁
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            rand = new Random();
            myboll = new MyBoll(this.ClientSize.Width, this.ClientSize.Height);
            for (int i = 0; i < bollnum; i++)
            {
                Boll boll = new Boll(this.ClientSize.Width, this.ClientSize.Height, rand);
                bolls.Add(boll);
            }
            timerball.Start();
            timertotal.Start();
        }

        private void Form1_Paint(object sender, PaintEventArgs e)
        {
            g = e.Graphics;
            /*foreach (Boll boll in bolls)
            {
                g.FillEllipse(sb1, boll.x, boll.y, boll.d, boll.d);
            }*/
            foreach (Boll boll in bolls)
            {
                g.DrawImage(image4, boll.x, boll.y, boll.d, boll.d);
            }
            g.DrawImage(image5, myboll.x, myboll.y, myboll.d, myboll.d);
            //g.FillEllipse(sb2, myboll.x, myboll.y, myboll.d, myboll.d);
        }

        private void timerball_Tick(object sender, EventArgs e)
        {
            foreach (Boll boll in bolls)
            {
                boll.Move();        
                double Y = myboll.y - boll.y;
                double X = myboll.x - boll.x;
                double R = X * X + Y * Y;
                if (R <Math.Pow(boll.d / 2 + myboll.d / 2,2))
                {
                    this.timerball.Stop();
                    this.timertotal.Stop();
                    MessageBox.Show("坚持了" + timer + "秒");
                    Database db = new Database();
                    string sql;
                    sql="insert into time1 values ('"+timer+"')";
                    DataSet ds = new DataSet();
                    ds = db.GetDataFromDB(sql);
                    DialogResult a = MessageBox.Show("是否再次挑战？", "提示", MessageBoxButtons.OKCancel);
                    if (a == DialogResult.OK)
                    {
                        Form1 form = new Form1();
                        form.Show();
                        this.Hide();
                    }
                    else
                    {
                        Login login = new Login();
                        login.Show();
                        this.Hide();
                    }
                }
            }
            this.Refresh();
        }
        #region 控制球的方向，上下左右的操作
        private void Form1_KeyDown(object sender, KeyEventArgs e)
        {
            switch (e.KeyCode)
            {
                case Keys.Up:
                case Keys.W:
                    timerup.Start();
                    break;
                case Keys.Down:
                case Keys.S:
                    timerdown.Start();
                    break;
                case Keys.Left:
                case Keys.A:
                    timerleft.Start();
                    break;
                case Keys.Right:
                case Keys.D:
                    timerright.Start();
                    break;
                default: break;
            }
        }

        private void Form1_KeyUp(object sender, KeyEventArgs e)
        {
            switch (e.KeyCode)
            {
                case Keys.Up:
                case Keys.W:
                    timerup.Stop();
                    break;
                case Keys.Down:
                case Keys.S:
                    timerdown.Stop();
                    break;
                case Keys.Left:
                case Keys.A:
                    timerleft.Stop();
                    break;
                case Keys.Right:
                case Keys.D:
                    timerright.Stop();
                    break;
                default: break;
            }
        }

        private void timerup_Tick(object sender, EventArgs e)
        {
            myboll.Up();
        }

        private void timerleft_Tick(object sender, EventArgs e)
        {
            myboll.Left();
        }

        private void timerright_Tick(object sender, EventArgs e)
        {
            myboll.Right();
        }

        private void timerdown_Tick(object sender, EventArgs e)
        {
            myboll.Down();
        }
        #endregion

        private void timertotal_Tick(object sender, EventArgs e)
        {
            timer++;
        }
    }
}
